
/* GCSx
** WSCROLL.H
**
** Widget parent class to provide frame/scrollbars
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_WSCROLL_H_
#define __GCSx_WSCROLL_H_

class WidgetScroll : public Widget {
public:
    enum FrameType {
        // Frame types (normally textbox background color)
        FRAMETYPE_BEVEL,
        FRAMETYPE_BEVEL_BK, // BKFILL background color
        FRAMETYPE_LINE,
        FRAMETYPE_NONE,
    };

protected:
    enum DragType {
        // Drag types
        FRAME_DRAG_NONE,
        FRAME_DRAG_CLIENT,
        FRAME_DRAG_SCROLLBAR_HORIZ,
        FRAME_DRAG_SCROLLBAR_VERT,
    };

    enum DragSubType {
        // Drag subtypes for scrollbar
        FRAME_SCROLL_NONE = 0,
        FRAME_SCROLL_LESS, // Left/Up
        FRAME_SCROLL_PAGELESS, // Page Left/Up
        FRAME_SCROLL_MORE, // Right/Down
        FRAME_SCROLL_PAGEMORE, // Page Right/Down
        FRAME_SCROLL_TAB,
    };

    enum {
        // Dirty reasons, can be OR'd
        FRAME_DIRTY_NONE = 0,
        FRAME_DIRTY_SCROLLVERT = 1,
        FRAME_DIRTY_SCROLLHORIZ = 2,
        FRAME_DIRTY_ALL = 255, // If resized or scroll existence changes
    };

    FrameType frameType;
    Window* clientArea;
    Widget* clientWidget; // NULL if client is a plain window
    int haveFocus;
    int whyDirty;
    
    int scrollbarButtonSize;
    int scrollbarButtonXPad;
    int frameThickness;
    
    // (width and position of our client area- not necessarily width/position
    //  of client window internally, which can be larger/smaller and scroll)
    // (main _includes_ scrollbars, Inner does not)
    int clientX;
    int clientY;
    int clientWidth;
    int clientHeight;
    int clientWidthInner;
    int clientHeightInner;
    
    // Width and height of client area
    // Reupdated whenever we resize()
    int clientActualWidth;
    int clientActualHeight;
    
    // Scroll offset (always zero or negative)
    int scrollX;
    int scrollY;
    
    // Scroll sizes- how much to scroll
    int vScrollLine;
    int vScrollPage;
    int hScrollLine;
    int hScrollPage;
    
    // Position to draw scrollbars; set to 0 if no scrollbars
    // Size of actual drawn scrollbars
    int vScrollbarX;
    int vScrollbarY;
    int vScrollbarWidth;
    int vScrollbarHeight;
    int vScrollbarTab; // Offset within scroll area
    int vScrollbarTabSize; // 0 = tab not shown
    int hScrollbarX;
    int hScrollbarY;
    int hScrollbarWidth;
    int hScrollbarHeight;
    int hScrollbarTab; // Offset within scroll area
    int hScrollbarTabSize; // 0 = tab not shown
    
    // Used for autorepeating scrollbar actions
    Uint32 scrollRepeatTicks;
    Uint32 scrollRepeatDelay;
    
    // Internal- for dragging titlebar, borders, etc.
    // hoverMode used just for hovering
    DragType dragMode;
    DragSubType dragItem;
    int dragX;
    int dragY;
    int dragItemOverflowX;
    int dragItemOverflowY;
    
    // Scrolls to a given -X, -Y
    // Automatically clips scroll to best possible
    // Can specify x and/or y for scrollbar tabs to force if needed
    void scrollTo(int sX, int sY, int tabX = -1, int tabY = -1);
    
    // Which area is a given coords in? titlebar, buttons, border, etc.
    // Returns a drag constant, and if applicable, a drag item (resize constant or button number)
    void whereCoords(int x, int y, DragType* dragType, DragSubType* dragItem) const;

    enum {
        // Sizing constants
        FRAME_BEVELTHICKNESS = 2,
        FRAME_LINETHICKNESS = 1,
        
        // Minimum size for scrollbar tab
        FRAME_SCROLLTABMIN = 10,

        // MS to wait before repeating scrollbar action initially
        DELAY_SCROLLBAR_REPEATSTART = 500,
        
        // MS to wait between additional repeats of scrollbar action
        DELAY_SCROLLBAR_REPEATAGAIN = 75,
    };
    
    static const std::string wtButtonUp;
    static const std::string wtButtonDown;
    static const std::string wtButtonLeft;
    static const std::string wtButtonRight;
    
    void construct(FrameType type, Window* client, int cWidth = -1, int cHeight = -1);

public:
    // Width and height represent client area and default to client's current size
    // These can easily be changed when you run show() to display the window
    // Client area pointer will be deleted by us if it wantsToBeDeleted().
    // We don't take ownership of pointer if constructor fails.
    // Client can be a widget or a regular window.
    WidgetScroll(FrameType type, Widget* client, int cWidth = -1, int cHeight = -1);
    WidgetScroll(FrameType type, Window* client, int cWidth = -1, int cHeight = -1, int cId = 0);
    ~WidgetScroll();
    
    // Client should call this whenever it resizes to update scrollbars
    // Default Window::resize() will call this if appropriate
    void updateClientSize();
    
    // Client may call this to set scrolling amounts; if not called,
    // a default size is used (page amounts are automatically
    // determined as the full width/height minus one "line")
    void setScroll(int vLine, int hLine);
    
    // Scrolls to show as much of the given rectangle as possible
    // Automatically clips scroll to best possible
    void scrollToView(int sX, int sY, int sWidth, int sHeight);
    // Scrolls to offset from current scroll
    void scrollBy(int sX, int sY);

    // this won't resize ourselves, but adjusts scroll as needed
    void newClient(Window* fClient);
    void newClient(Widget* fClient);
    void newClient(); // (Sets as no client)
    
    void resize(int newWidth, int newHeight, int newViewWidth = -1, int newViewHeight = -1, int fromParent = 0);
    void display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset);
    
    int event(int hasFocus, const SDL_Event* event);

    int getClientWidth() const { return clientWidth; }
    int getClientHeight() const { return clientHeight; }

    // These all simply pass down to client
    int stateTabId() const;
    int stateTabId(int wTabId);
    void doAction();
    void disable();
    void enable();
    int refuseAll() const;
    void load();
    void apply();
    int getId() const;
    char getShortcut() const;
    void resolutionChange(int fromW, int fromH, int fromBpp, int toW, int toH, int toBpp);
    void childMoved(int fromX, int fromY, int toX, int toY, Window* child);
    void childResized(int fromW, int fromH, int toW, int toH, Window* child);
    const char* tooltip(int xPos, int yPos) const;
    Widget* returnSelf();
};

#endif

